package com.supermap.business.common;

import com.alibaba.dubbo.config.annotation.Reference;
import com.supermap.dubbo.api.common.entity.Crop;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dubbo.api.common.CropService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author:langhongshuai
 * @Description:作物接口层
 * @Date:18/1/8 上午11:26
 */
@Controller
@RequestMapping("CropController")
public class CropController {
    /**
     * 使用指定类初始化日志对象，在日志输出的时候，可以打印出日志信息所在类
     */
    protected static final Logger logger = LoggerFactory.getLogger(CropController.class);

    @Reference
    private CropService cropService;

    /**
     * 当levels为2时，获取科目，当levels为3时，获取作物，当levels为0时，获取科目以及下属作物
     */
    @RequestMapping("/getAllCrops")
    @ResponseBody
    public JSONUtil getAllAreas(HttpServletRequest request){
        String levels = request.getParameter("levels");
        return cropService.getAllCrops(Integer.parseInt(levels));
    }

    /**
     * 保存作物
     */
    @RequestMapping("/saveCrop")
    @ResponseBody
    public JSONUtil saveCrop(HttpServletRequest request){
        String id = request.getParameter("id");
        String parentid = request.getParameter("parentid");
        String code =request.getParameter("code");
        String name = request.getParameter("name");
        String levels = request.getParameter("levels");
        String description = request.getParameter("description");
        String imgico = request.getParameter("imgIcon");
        Crop crop = new Crop();
        crop.setId(Integer.parseInt(id));
        crop.setCode(code);
        crop.setParentid(Integer.parseInt(parentid));
        crop.setName(name);
        crop.setLevel(Integer.parseInt(levels));
        crop.setDescription(description);
        crop.setImgIco(imgico);
        return cropService.saveCrop(crop);
    }

    /**
     * 更新作物
     */
    @RequestMapping("updateCrop")
    @ResponseBody
    public JSONUtil updateCrop(HttpServletRequest request){
        String id = request.getParameter("id");
        String parentid = request.getParameter("parentid");
        String code =request.getParameter("code");
        String name = request.getParameter("name");
        String levels = request.getParameter("levels");
        String description = request.getParameter("description");
        String imgIco = request.getParameter("imgIcon");
        Crop crop = new Crop();
        crop.setId(Integer.parseInt(id));
        crop.setCode(code);
        crop.setParentid(Integer.parseInt(parentid));
        crop.setName(name);
        crop.setLevel(Integer.parseInt(levels));
        crop.setDescription(description);
        crop.setImgIco(imgIco);
        return cropService.updateCrop(crop);
    }

    /**
     * 删除作物
     */
    @RequestMapping("deleteCrop")
    @ResponseBody
    public JSONUtil deleteCrop(HttpServletRequest request){
        String id = request.getParameter("id");
        return cropService.deleteCrop(id);
    }

    /**
     * 增加用户作物定制
     * @param userId
     * @param cropCode
     * @return
     */
    @RequestMapping("addUserCrop")
    @ResponseBody
    public JSONUtil addUserCrop(String userId,String cropCode){
        return cropService.addUserCrop(userId,cropCode);
    }

    /**
     * 删除用户作物定制
     * @param userId
     * @param cropCode
     * @return
     */
    @RequestMapping("deleteUserCrop")
    @ResponseBody
    public JSONUtil deleteUserCrop(String userId,String cropCode){
        return cropService.deleteUserCrop(userId,cropCode);
    }

    /**
     * 获取用户作物定制情况
     * @param userId
     * @return
     */
    @RequestMapping(value = "getUserCrops",method = RequestMethod.GET)
    @ResponseBody
    public JSONUtil getUserCrops(String userId){
        return cropService.getUserCrops(userId);
    }

}
